
%This code calculates mixed strategy equilibria for any council size K, number of
%locations N and ideal policies of parties L and R. Mixed strategy equilibria are calculated using
%the Lemke-Howson algorithm. That is, platform.m calls LemkeHowson.m that
%for ease of reference is also included in this folder. The version
%included in this replication package is  Retrieved on February 29, 2024 and cited as follows: Richard Katzwer (2024).
%Lemke-Howson Algorithm for 2-Player Games
%(https://www.mathworks.com/matlabcentral/fileexchange/44279-lemke-howson-algorithm-for-2-player-games),
%MATLAB Central File Exchange. The license.txt file in the folder contains
%the Copyright statement of LemkeHowson.m.

function [weightsL, ExpectedWidthL,ExpectedWidthR,weights,PayoffL,PayoffR] = platform(K, N, IdealR, IdealL)


for n=N % THIS IS NUMBER OF LOCATIONS
    
        ML=zeros(n,1); % THIS IS THE MATRIX WE ARE GETTING FOR AVAILABLE MODERATE LOCATIONS
        EL=zeros(n,1); % THIS IS THE MATRIX WE ARE GETTING FOR THE OPTIMAL EXTREME LOCATIONS
      
    for l=IdealL % this is the ideal policy of the left party
    for i=1:n      
 for j=1
     ML(i,j)=(i-1)/(n-1);
     EL(i,j)=0.5*(3*l-((i-1)/(n-1)));
 end
    end
   
    for i=1:n      
 for j=1
     if ML(i,j)>=0.5
     ML(i,j)=-100;
     EL(i,j)=-100;
     end
 end
    end
  for i=1:n      
 for j=1
     if ML(i,j)<l
     ML(i,j)=-100;
     EL(i,j)=-100;
     end
 end
    end
    end
    
   MR=zeros(n,1); % THIS IS THE MATRIX WE ARE GETTING FOR AVAILABLE MODERATE LOCATIONS
        ER=zeros(n,1); % THIS IS THE MATRIX WE ARE GETTING FOR THE OPTIMAL EXTREME LOCATIONS
      
    for r=IdealR % this is the ideal policy of the right party
    for i=1:n      
 for j=1
     MR(i,j)=(i-1)/(n-1);
     ER(i,j)=0.5*(3*r-((i-1)/(n-1)));
 end
    end
   
    for i=1:n      
 for j=1
     if MR(i,j)<=0.5
     MR(i,j)=-100;
     ER(i,j)=-100;
     end
 end
    end
  for i=1:n      
 for j=1
     if MR(i,j)>r
     MR(i,j)=-100;
     ER(i,j)=-100;
     end
 end
    end
    end    
 
  
end

   ML(ML(:, 1)== -100, :)= [];
   MR(MR(:, 1)== -100, :)= [];
   EL(EL(:, 1)== -100, :)= [];
   ER(ER(:, 1)== -100, :)= [];
   ML=flip(ML); % this makes the most moderate locations appear on top, just flips ML order
   EL=flip(EL);

   
   [d,t]=size(ML) ;% d is number of available meaningful policies in the discrete space to create the relavant vote, seat, and payoff matrices
   
   Indif=zeros((d+2),(d+2)) ;
   for i=3:d+2     
 
     Indif(i,2)=ML(i-2,1) ;% second column in votes is location of moderate left
     Indif(i,1)=EL(i-2,1) ;% first column in votes is location of extreme left
     
   end
 
    for j=3:d+2     
 
     Indif(2,j)=MR(j-2,1) ;% second row in votes is location of moderate right
     Indif(1,j)=ER(j-2,1) ;% first row in votes is location of extreme right
     
    end
 
    for i=3:d+2
     for j=3:d+2     
 
     Indif(i,j)=(Indif(i,2)+Indif(2,j))/2 ;% second row in votes is location of moderate right
     
 end
    end
   
    
    SL=Indif;
    
        for i=3:d+2
     for j=3:d+2     
 
         if Indif(i,j)~=0 && Indif(i,j)~=0.5  && Indif(i,j)~=1
       for k=K %HERE WE NEED TO SPECIFY SIZE OF COUNCIL K. THE FOLLOWINS IS D'HONDT
        for a=1:k-1
     if Indif(i,j)~=0.5 && Indif(i,j)~=0 && Indif(i,j)~=1 && ((a)/(k+1))<Indif(i,j) && Indif(i,j)<((a+1)/(k+1))
      SL(i,j)=a/k ;
     end   
     if Indif(i,j)~=0.5 && Indif(i,j)~=0 && Indif(i,j)~=1 && Indif(i,j)>((a+1)/(k+1))
      SL(i,j)=1 ;
     end
     
        end
        for a=1:k % This is for seats where vote share is equal to the threshold. we give 0.5 low and high step
     
     if Indif(i,j)~=0.5 && Indif(i,j)~=0 && Indif(i,j)~=1 && Indif(i,j)==((a)/(k+1))
      SL(i,j)=0.5*((a)/(k))+0.5*((a-1)/(k));
     end    
        end
     end
         end
 end
        end
    
    PayoffL(:,:)=SL(:,:);
    
    funcL = @(x) (x-l).^2;
    
    for i=3:d+2
     for j=3:d+2 
         PayoffL(i,j)=SL(i,j)*(-(1)/(SL(i,2)-SL(i,1)))*integral(funcL,SL(i,1),SL(i,2)) +(1-SL(i,j))*(-(1)/(SL(1,j)-SL(2,j)))*integral(funcL,SL(2,j),SL(1,j)); 
     end
    end
    
    PayoffR(:,:)=SL(:,:);
    
    funcR = @(x) (x-r).^2;
    
    for i=3:d+2
     for j=3:d+2 
         PayoffR(i,j)=SL(i,j)*(-(1)/(SL(i,2)-SL(i,1)))*integral(funcR,SL(i,1),SL(i,2)) +(1-SL(i,j))*(-(1)/(SL(1,j)-SL(2,j)))*integral(funcR,SL(2,j),SL(1,j)); 
     end
    end
    
    L(:,:)=PayoffL(:,:); % these are the payoff matrixes for all relevant strategies to be used for the mixed
     R(:,:)=PayoffR(:,:);
     
     L([1,2],:) = []; % Here I drop from L and R the two first columns and rows
     R([1,2],:) = [];
      L(:,[1,2]) = [];
     R(:,[1,2]) = [];
     
     StrategiesR=PayoffR(:,:);
     StrategiesL=PayoffL(:,:);
     
        StrategiesR(3:d+2,:) = []; % Here I keep from the Payoff matrixes only the available strategies
          StrategiesR(:,1:2) = [];
        StrategiesL(1:2,:) = [];
     StrategiesL(:,3:d+2) = [];
     
      StrategiesR=transpose(StrategiesR);
     
      WidthL= StrategiesL(1:d,2)-StrategiesL(1:d,1);
     WidthR= StrategiesR(1:d,1)-StrategiesR(1:d,2);
     
nashEqbm = LemkeHowson(L, R);
weights=cell2mat(nashEqbm);
for i=1:d
weights(i,2)=weights(i+d,1);
end
 weights(d+1:d+d,:) = [] ;
  
 weightsL=weights(:,1);
 weightsR=weights(:,2);
 
 weightsL=transpose(weightsL);
 weightsR=transpose(weightsR);
ExpectedWidthL=weightsL*WidthL;
ExpectedWidthR=weightsR*WidthR;
%end